﻿package mortal.game.view.common.cd
{
    import com.gengine.core.call.*;
    import com.gengine.core.frame.*;
    import flash.utils.*;
    import mortal.game.view.common.cd.effect.*;

    public class CDData extends Object implements ICDData
    {
        protected var _isCoolDown:Boolean = false;
        protected var _timer:FrameTimer;
        protected var _caller:Caller;
        protected var _beginTime:Number = 0;
        protected var _totalTime:Number = 0;
        protected var _lastFrame:int = 0;
        protected var _lastSecond:int = 0;
        protected var _lastPercentage:int = 0;
        protected var _percentage:int;
        protected var _second:int;
        protected var _key:String;
        protected var _usedTime:int = 0;

        public function CDData()
        {
            this._caller = new Caller();
            this._timer = new FrameTimer(1);
            this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            return;
        }// end function

        public function set key(param1:String) : void
        {
            this._key = param1;
            return;
        }// end function

        public function get key() : String
        {
            return this._key;
        }// end function

        public function addEffect(param1:ICDEffect) : void
        {
            if (param1.registed)
            {
                return;
            }
            this._caller.addCall(param1.cdEffectTimerType, param1.onTimer);
            param1.registed = true;
            if (this._isCoolDown)
            {
                this.resetLastTime();
                this.onEnterFrameHandler(null);
            }
            return;
        }// end function

        public function removeEffect(param1:ICDEffect) : void
        {
            if (!param1.registed)
            {
                return;
            }
            this._caller.removeCall(param1.cdEffectTimerType, param1.onTimer);
            param1.registed = false;
            return;
        }// end function

        public function addFinishCallback(param1:Function) : void
        {
            this._caller.addCall(CDEffectTimerType.FinishedCallback, param1);
            if (this._isCoolDown)
            {
                this.resetLastTime();
                this.onEnterFrameHandler(null);
            }
            return;
        }// end function

        public function removeFinishCallback(param1:Function) : void
        {
            this._caller.removeCall(CDEffectTimerType.FinishedCallback, param1);
            return;
        }// end function

        public function addFrameUpdate(param1:Function) : void
        {
            this._caller.addCall(CDEffectTimerType.FrameUpdateCallback, param1);
            return;
        }// end function

        public function removeFrameUpdate(param1:Function) : void
        {
            this._caller.removeCall(CDEffectTimerType.FrameUpdateCallback, param1);
            return;
        }// end function

        public function addStartCallback(param1:Function) : void
        {
            this._caller.addCall(CDEffectTimerType.StartCallback, param1);
            return;
        }// end function

        public function removeStartCallback(param1:Function) : void
        {
            this._caller.removeCall(CDEffectTimerType.StartCallback, param1);
            return;
        }// end function

        public function startCoolDown() : void
        {
            if (this._isCoolDown)
            {
                return;
            }
            this._isCoolDown = true;
            this._usedTime = 0;
            this.resetLastTime();
            if (this.beginTime == 0)
            {
                this.beginTime = getTimer();
            }
            this._timer.start();
            this._caller.call(CDEffectTimerType.StartCallback);
            return;
        }// end function

        protected function resetLastTime() : void
        {
            this._lastFrame = -1;
            this._lastSecond = -1;
            this._lastPercentage = -1;
            return;
        }// end function

        public function stopCoolDown() : void
        {
            this.setViewToEnd();
            this.resetLastTime();
            this.beginTime = 0;
            this._usedTime = 0;
            this._isCoolDown = false;
            this._timer.stop();
            return;
        }// end function

        public function get beginTime() : Number
        {
            return this._beginTime;
        }// end function

        public function set beginTime(param1:Number) : void
        {
            this._beginTime = param1;
            return;
        }// end function

        public function set totalTime(param1:Number) : void
        {
            this._totalTime = param1;
            return;
        }// end function

        public function get totalTime() : Number
        {
            return this._totalTime;
        }// end function

        public function get isCoolDown() : Boolean
        {
            return this._isCoolDown;
        }// end function

        public function set isCoolDown(param1:Boolean) : void
        {
            this._isCoolDown = param1;
            return;
        }// end function

        public function get caller() : Caller
        {
            return this._caller;
        }// end function

        public function get leftTime() : int
        {
            return this._totalTime - this._usedTime;
        }// end function

        public function get usedTime() : int
        {
            return this._usedTime;
        }// end function

        protected function onEnterFrameHandler(param1:BaseTimer) : void
        {
            this._usedTime = FrameManager.flashFrame.getTime() - this.beginTime;
            if (this._usedTime > this.totalTime)
            {
                this._timer.stop();
                this._timer.reset();
                this.stopCoolDown();
                this._caller.call(CDEffectTimerType.FinishedCallback);
                return;
            }
            this.callPercentage(this._usedTime);
            this.callFrame(this._usedTime);
            this.callSecond(this._usedTime);
            this._caller.call(CDEffectTimerType.FrameUpdateCallback);
            return;
        }// end function

        protected function callPercentage(param1:int) : void
        {
            this._percentage = Math.ceil(param1 / this.totalTime * 100);
            if (this._percentage != this._lastPercentage)
            {
                this._lastPercentage = this._percentage;
                this._caller.call(CDEffectTimerType.Percentage, this._percentage);
            }
            return;
        }// end function

        protected function callFrame(param1:int) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this._lastFrame + 1;
            _loc_2._lastFrame = _loc_3;
            this._caller.call(CDEffectTimerType.Frame, this._lastFrame);
            return;
        }// end function

        protected function callSecond(param1:int) : void
        {
            this._second = Math.ceil((this.totalTime - param1) / 1000);
            if (this._second != this._lastSecond)
            {
                this._lastSecond = this._second;
                this._caller.call(CDEffectTimerType.Second, this._second);
            }
            return;
        }// end function

        protected function setViewToEnd() : void
        {
            if (this._caller)
            {
                this._caller.call(CDEffectTimerType.Percentage, 101);
                this._caller.call(CDEffectTimerType.Second, 0);
                this._caller.call(CDEffectTimerType.Frame, 0);
            }
            return;
        }// end function

        public function dispose() : void
        {
            this.stopCoolDown();
            if (this._caller)
            {
                this._caller.dispose();
            }
            this._timer.dispose();
            return;
        }// end function

    }
}
